require(["config"],function(){
    require(["jquery", "swiper", "artTemplate","cookie","tools"], function ($, Swiper, template) {
        class IndexPage{
            constructor(){
                this.cart = [];
                this.initSwiper();
                this.render();
                this.rendershop();
                this.rendershow();
                this.bindEventListener();
                $.cookie.json = true
                this.loadCart();
                this.shopCart();
            }
            initSwiper(){
                const { id } = $.qsToObj(location.search.slice(1));
                $.ajax({
                    url:`http://www.xiongmaoyouxuan.com/api/detail?id=${id}&normal=1&sa=`,
                    method:"GET",
                    data:{
                        type:0
                    },
                    hedaers:{
                        "x-platform":"pc"
                    },
                    success:resData => {
                        // console.log(resData);
                        const { photo } = resData.data.detail;
                        const html = template("slide-template",{
                            slides:photo
                        })
                        $(".swiper-wrapper").html(html)
                        new Swiper(".swiper-container",{
                            loop:true,
                            autoplay: {
                                disableOnInteraction: false,
                                delay:2000,
                              },
                            pagination: {
                                el: '.swiper-pagination',
                                clickable: true
                            },
                        })
                    },
                    error:err => {
                        console.log("失败",err);
                    },
                    complete:data => {

                    }
                })
            }
            //渲染数据
            render(){
                const { id } = $.qsToObj(location.search.slice(1));
                const m = $.ajax({
                    url:`http://www.xiongmaoyouxuan.com/api/detail?id=${id}&normal=1&sa=`,
                    dadaType:"json",
                    success(resData){
                        let infos = resData.data.detail;
                        const html = template("details-template",{infos})
                        $(".container").html(html);
                    },
                    async:false
                })
                return m.responseJSON.data.detail;
            }
            rendershop(){
                const { id } = $.qsToObj(location.search.slice(1));
                $.ajax({
                    url:`http://www.xiongmaoyouxuan.com/api/detail?id=${id}&normal=1&sa=`,
                    dadaType:"json",
                    success(resData){
                        let shopper = resData.data.shop;
                        const html = template("shop-template",{shopper})
                        $(".shop-info").html(html);
                    }
                })
            }
            rendershow(){
                const { id } = $.qsToObj(location.search.slice(1));
                $.ajax({
                    url:`http://www.xiongmaoyouxuan.com/api/detail?id=${id}&normal=1&sa=`,
                    dadaType:"json",
                    success(resData){
                        let picshow = resData.data.detail.descContentList;
                        // console.log(picshow);
                        const html = template("show-template",{picshow})
                        $(".cmd-details > .imgs").html(html);
                    }
                })
            }
            bindEventListener(){
                $(".container").on("click",".goTB-btn",e=>{
                    const detail = this.render()
                    e.preventDefault()
                    //构建当前选购商品对象
                    const currentshop = {
                        id:detail.id,
                        title:detail.title,
                        originprice:detail.originPrice,
                        nowprice:detail.price,
                        image:detail.image,
                        amount:1,
                        checked:true
                    }
                    console.log(currentshop);
                    //获取已有购物车数组
                    const cart = $.cookie("cart")|| []
                    //判断数据是否存在，如果存在，则叠加商品数量
                    const has = cart.some(item => item.id === currentshop.id)
                    if(has) {
                        for(let i = 0, len = cart.length; i < len; i++){
                            if(cart[i].id === currentshop.id){
                                cart[i].amount += currentshop.amount
                                break
                            }
                        }
                    }else{
                        cart.push(currentshop);
                    }
                    $.cookie("cart",cart,{path:'/',expires:10})
                })
            }
              loadCart(){
                this.cart = $.cookie("cart") || []
            }
            //购物车按钮中的数据
            shopCart(){
                const len = this.cart.length;
                console.log(len);
                $(".cart-num").html(len);
            }
        }
        new IndexPage()
    })
})